package javolution;

import javolution.context.ArrayFactory;
import javolution.context.ConcurrentContext;
import javolution.context.LocalContext;
import javolution.context.ObjectFactory;
import javolution.context.StackContext;
import javolution.lang.MathLib;
import javolution.testing.TestCase;
import javolution.testing.TestContext;
import javolution.testing.TestSuite;
import javolution.text.TextBuilder;
import javolution.util.FastTable;
import javolution.util.Index;

/* loaded from: classes2.dex */
public final class ContextTestSuite extends TestSuite {

    /* loaded from: classes2.dex */
    public static class ArrayAllocation extends TestCase {
        char[] _array;
        boolean _recycle;
        int _size;

        public ArrayAllocation(int i, boolean z) {
            this._size = i;
            this._recycle = z;
        }

        @Override // javolution.testing.TestCase
        public void execute() {
            if (!this._recycle) {
                this._array = new char[this._size];
                return;
            }
            this._array = ArrayFactory.CHARS_FACTORY.array(this._size);
            ArrayFactory.CHARS_FACTORY.recycle(this._array);
        }

        @Override // javolution.testing.TestCase
        public CharSequence getDescription() {
            TextBuilder append;
            String str;
            if (this._recycle) {
                append = TextBuilder.newInstance().append("Recycled - ArrayFactory.CHARS_FACTORY.array(").append(this._size);
                str = ")";
            } else {
                append = TextBuilder.newInstance().append("HeapContext (default)            - new char[").append(this._size);
                str = "]";
            }
            return append.append(str);
        }

        @Override // javolution.testing.TestCase
        public void validate() {
            TestContext.assertTrue(this._array.length >= this._size);
        }
    }

    /* loaded from: classes2.dex */
    public static class Concurrency extends TestCase {
        final int _concurrency;
        final int _size;
        FastTable _table;

        public Concurrency(int i, int i2) {
            this._size = i;
            this._concurrency = i2;
        }

        /* JADX INFO: Access modifiers changed from: private */
        public void quickSort(final FastTable fastTable) {
            final int size = fastTable.size();
            if (size < 100) {
                fastTable.sort();
                return;
            }
            final FastTable newInstance = FastTable.newInstance();
            final FastTable newInstance2 = FastTable.newInstance();
            ConcurrentContext.enter();
            try {
                ConcurrentContext.execute(new Runnable() { // from class: javolution.ContextTestSuite.Concurrency.1
                    @Override // java.lang.Runnable
                    public void run() {
                        newInstance.addAll(fastTable.subList(0, size / 2));
                        Concurrency.this.quickSort(newInstance);
                    }
                });
                ConcurrentContext.execute(new Runnable() { // from class: javolution.ContextTestSuite.Concurrency.2
                    @Override // java.lang.Runnable
                    public void run() {
                        FastTable fastTable2 = newInstance2;
                        FastTable fastTable3 = fastTable;
                        int i = size;
                        fastTable2.addAll(fastTable3.subList(i / 2, i));
                        Concurrency.this.quickSort(newInstance2);
                    }
                });
                ConcurrentContext.exit();
                int i = 0;
                int i2 = 0;
                for (int i3 = 0; i3 < size; i3++) {
                    if (i >= newInstance.size()) {
                        fastTable.set(i3, newInstance2.get(i2));
                        i2++;
                    } else if (i2 >= newInstance2.size()) {
                        fastTable.set(i3, newInstance.get(i));
                        i++;
                    } else {
                        Index index = (Index) newInstance.get(i);
                        Index index2 = (Index) newInstance2.get(i2);
                        if (index.intValue() < index2.intValue()) {
                            fastTable.set(i3, index);
                            i++;
                        } else {
                            fastTable.set(i3, index2);
                            i2++;
                        }
                    }
                }
                FastTable.recycle(newInstance);
                FastTable.recycle(newInstance2);
            } catch (Throwable th) {
                ConcurrentContext.exit();
                throw th;
            }
        }

        @Override // javolution.testing.TestCase
        public void execute() {
            LocalContext.enter();
            try {
                ConcurrentContext.setConcurrency(this._concurrency);
                quickSort(this._table);
            } finally {
                LocalContext.exit();
            }
        }

        @Override // javolution.testing.TestCase
        public CharSequence getDescription() {
            return TextBuilder.newInstance().append("ConcurrentContext - Quick Sort (size: ").append(this._size).append(", concurrency: ").append(this._concurrency).append(")");
        }

        @Override // javolution.testing.TestCase
        public void prepare() {
            this._table = new FastTable(this._size);
            int i = 0;
            while (true) {
                int i2 = this._size;
                if (i >= i2) {
                    return;
                }
                this._table.add(Index.valueOf(MathLib.random(0, i2)));
                i++;
            }
        }

        @Override // javolution.testing.TestCase
        public void validate() {
            int intValue;
            TestContext.assertEquals("Size", new Integer(this._size), new Integer(this._table.size()));
            int i = 0;
            do {
                if (i >= this._size - 1) {
                    return;
                }
                intValue = ((Index) this._table.get(i)).intValue();
                i++;
            } while (TestContext.assertTrue("Natural Order", intValue <= ((Index) this._table.get(i)).intValue()));
        }
    }

    /* loaded from: classes2.dex */
    public static class SmallObjectAllocation extends TestCase {
        int _size;
        boolean _useStack;
        XYZ c0;
        XYZ c1;

        /* loaded from: classes2.dex */
        private static final class XYZ {
            static ObjectFactory FACTORY = new ObjectFactory() { // from class: javolution.ContextTestSuite.SmallObjectAllocation.XYZ.1
                /* JADX INFO: Access modifiers changed from: protected */
                @Override // javolution.context.ObjectFactory
                public Object create() {
                    return new XYZ();
                }
            };
            double x;
            double y;
            double z;

            private XYZ() {
            }

            public XYZ(double d, double d2, double d3) {
                this.x = d;
                this.y = d2;
                this.z = d3;
            }

            public static XYZ valueOf(double d, double d2, double d3) {
                XYZ xyz = (XYZ) FACTORY.object();
                xyz.x = d;
                xyz.y = d2;
                xyz.z = d3;
                return xyz;
            }

            public XYZ plusHeap(XYZ xyz) {
                return new XYZ(this.x + xyz.x, this.y + xyz.y, this.z + xyz.z);
            }

            public XYZ plusStack(XYZ xyz) {
                return valueOf(this.x + xyz.x, this.y + xyz.y, this.z + xyz.z);
            }
        }

        public SmallObjectAllocation(int i, boolean z) {
            this._size = i;
            this._useStack = z;
        }

        @Override // javolution.testing.TestCase
        public void cleanup() {
            StackContext.exit();
        }

        @Override // javolution.testing.TestCase
        public int count() {
            return this._size;
        }

        @Override // javolution.testing.TestCase
        public void execute() {
            int i = 0;
            if (this._useStack) {
                while (i < this._size) {
                    XYZ plusStack = this.c1.plusStack(this.c0);
                    this.c0 = this.c1;
                    this.c1 = plusStack;
                    i++;
                }
                return;
            }
            while (i < this._size) {
                XYZ plusHeap = this.c1.plusHeap(this.c0);
                this.c0 = this.c1;
                this.c1 = plusHeap;
                i++;
            }
        }

        @Override // javolution.testing.TestCase
        public CharSequence getDescription() {
            return TextBuilder.newInstance().append(!this._useStack ? "HeapContext (default) - Create " : "StackContext            Create ").append(this._size).append(" small objects");
        }

        @Override // javolution.testing.TestCase
        public void prepare() {
            this.c0 = new XYZ(0.0d, 0.0d, 0.0d);
            this.c1 = new XYZ(-1.0d, 1.0d, 0.0d);
            StackContext.enter();
        }

        @Override // javolution.testing.TestCase
        public void validate() {
            TestContext.assertTrue(this.c0.x == (-this.c0.y));
        }
    }

    @Override // javolution.testing.TestSuite, java.lang.Runnable
    public void run() {
        TestContext.info("-------------------------------------------------");
        TestContext.info("-- Test Suite for package javolution.context.* --");
        TestContext.info("-------------------------------------------------");
        int concurrency = ConcurrentContext.getConcurrency();
        TestContext.test(new Concurrency(10000, 0));
        TestContext.test(new Concurrency(10000, concurrency));
        TestContext.info("");
        TestContext.test(new SmallObjectAllocation(1000, false));
        TestContext.test(new SmallObjectAllocation(1000, true));
        TestContext.info("");
        TestContext.test(new ArrayAllocation(4096, false));
        TestContext.test(new ArrayAllocation(4096, true));
        TestContext.info("");
    }
}
